package com.gluck.cases.core.block.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.dal.dao.CasesTestsMapper;
import com.gluck.cases.core.block.CasesTestsBlock;
import com.gluck.cases.core.modal.assemble.CasesTestsAssemble;
import com.gluck.cases.core.modal.assemble.PageInfoAssemble;
import com.gluck.cases.core.modal.dto.CasesTestsDTO;
import com.gluck.cases.core.modal.entity.CasesTests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CasesTestsBlockImpl
 * @Desc CasesTestsBlock 实现类
 * @Author shiyongzhe
 * @Date 2022/6/3 - 2:09 下午
 **/
@Service
public class CasesTestsBlockImpl implements CasesTestsBlock {

    @Autowired
    private CasesTestsMapper casesTestsMapper;

    @Override
    public PageInfo<CasesTestsDTO> queryCasesTests(Long gid, Integer startPage, Integer endPage) {
        PageInfo<CasesTests> pageInfo = new PageInfo<>(PageHelper.startPage(startPage, endPage).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                casesTestsMapper.queryTestsByGroupId(gid);
            }
        }));
        List<CasesTestsDTO> dtos = pageInfo.getList().stream().map(x -> {
            CasesTestsDTO dto = CasesTestsAssemble.CasesTestConvertDTO(x);
            return dto;
        }).collect(Collectors.toList());
        PageInfoAssemble<CasesTestsDTO> assemble = new PageInfoAssemble<CasesTestsDTO>();
        PageInfo<CasesTestsDTO> result = assemble.covertDtoPageInfo(pageInfo, dtos);
        return result;
    }

    @Override
    public boolean addCasesTest(CasesTestsDTO dto) {
        CasesTests casesTests = CasesTestsAssemble.CasesTestDTOConvertCasesTest(dto);
        int insert = casesTestsMapper.addCasesTest(casesTests);
        return insert > 0;
    }

    @Override
    public boolean deleteCasesTestById(Long id) {
        int delete =  casesTestsMapper.deleteCasesTestById(id);
        return delete > 0;
    }

    @Override
    public boolean deleteCasesTestByGid(Long gid) {
        int delete = casesTestsMapper.deleteCasesTestByGid(gid);
        return delete > 0;
    }

    @Override
    public boolean updateCasesTest(CasesTestsDTO dto) {
        int update = casesTestsMapper.updateCasesTest(CasesTestsAssemble.CasesTestDTOConvertCasesTest(dto));
        return update > 0;
    }
}
